from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, 
                               QLineEdit, QSpinBox, QComboBox, QPushButton, 
                               QGroupBox, QRadioButton, QScrollArea, QFileDialog)
from PyQt6.QtCore import Qt

class TaskConfigWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        # 创建主布局
        main_layout = QVBoxLayout()
        self.setLayout(main_layout)
        
        # 创建滚动区域
        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        main_layout.addWidget(scroll)
        
        # 创建容器widget
        container = QWidget()
        scroll.setWidget(container)
        layout = QVBoxLayout(container)
        
        # 1. 视频背景设置
        bg_group = QGroupBox("视频背景设置")
        bg_layout = QVBoxLayout()
        
        # 预设音乐
        music_label = QLabel("预设音乐:")
        self.music_combo = QComboBox()
        self.music_combo.addItems(["音乐1", "音乐2", "音乐3"])  # 示例音乐
        self.preview_btn = QPushButton("预览")
        
        music_layout = QHBoxLayout()
        music_layout.addWidget(music_label)
        music_layout.addWidget(self.music_combo)
        music_layout.addWidget(self.preview_btn)
        bg_layout.addLayout(music_layout)
        
        # 自定义音乐
        self.custom_music_btn = QPushButton("上传自定义音乐")
        self.custom_music_btn.clicked.connect(self.upload_music)
        bg_layout.addWidget(self.custom_music_btn)
        
        bg_group.setLayout(bg_layout)
        layout.addWidget(bg_group)
        
        # 2. 图片参数
        img_group = QGroupBox("图片参数")
        img_layout = QVBoxLayout()
        
        # 主题设置
        theme_layout = QHBoxLayout()
        theme_layout.addWidget(QLabel("主题设置:"))
        self.theme_input = QLineEdit()
        theme_layout.addWidget(self.theme_input)
        img_layout.addLayout(theme_layout)
        
        # 生成要求
        req_layout = QHBoxLayout()
        req_layout.addWidget(QLabel("生成要求:"))
        self.req_input = QLineEdit()
        req_layout.addWidget(self.req_input)
        img_layout.addLayout(req_layout)
        
        # 图片数量
        count_layout = QHBoxLayout()
        count_layout.addWidget(QLabel("图片数量:"))
        self.count_spin = QSpinBox()
        self.count_spin.setMinimum(1)
        self.count_spin.setValue(1)
        count_layout.addWidget(self.count_spin)
        count_layout.addStretch()
        img_layout.addLayout(count_layout)
        
        # 图片尺寸
        size_layout = QHBoxLayout()
        size_layout.addWidget(QLabel("图片尺寸:"))
        self.size_combo = QComboBox()
        self.size_combo.addItems(["512x512", "768x768", "1024x1024"])
        size_layout.addWidget(self.size_combo)
        size_layout.addStretch()
        img_layout.addLayout(size_layout)
        
        # 图片模型
        model_layout = QHBoxLayout()
        model_layout.addWidget(QLabel("图片模型:"))
        self.model_combo = QComboBox()
        self.model_combo.addItems(["星流star-3 Alpha"])  # 示例模型
        model_layout.addWidget(self.model_combo)
        self.add_model_btn = QPushButton("添加图片模型")
        model_layout.addWidget(self.add_model_btn)
        img_layout.addLayout(model_layout)
        
        img_group.setLayout(img_layout)
        layout.addWidget(img_group)
        
        # 3. 视频参数
        video_group = QGroupBox("视频参数")
        video_layout = QVBoxLayout()
        
        # 视频尺寸
        v_size_layout = QHBoxLayout()
        v_size_layout.addWidget(QLabel("视频尺寸:"))
        self.v_size_combo = QComboBox()
        self.v_size_combo.addItems(["1920x1080", "1280x720", "854x480"])
        v_size_layout.addWidget(self.v_size_combo)
        v_size_layout.addStretch()
        video_layout.addLayout(v_size_layout)
        
        # 帧率
        fps_layout = QHBoxLayout()
        fps_layout.addWidget(QLabel("帧率:"))
        self.fps_spin = QSpinBox()
        self.fps_spin.setRange(24, 60)
        self.fps_spin.setValue(30)
        fps_layout.addWidget(self.fps_spin)
        fps_layout.addStretch()
        video_layout.addLayout(fps_layout)
        
        # 视频质量
        quality_layout = QHBoxLayout()
        quality_layout.addWidget(QLabel("视频质量:"))
        self.quality_combo = QComboBox()
        self.quality_combo.addItems(["高质量", "中等质量", "低质量"])
        quality_layout.addWidget(self.quality_combo)
        quality_layout.addStretch()
        video_layout.addLayout(quality_layout)
        
        video_group.setLayout(video_layout)
        layout.addWidget(video_group)
        
        # 4. 剪辑参数
        edit_group = QGroupBox("剪辑参数")
        edit_layout = QVBoxLayout()
        
        # 最小片段时长
        min_dur_layout = QHBoxLayout()
        min_dur_layout.addWidget(QLabel("最小片段时长(秒):"))
        self.min_dur_spin = QSpinBox()
        self.min_dur_spin.setRange(1, 60)
        self.min_dur_spin.setValue(3)
        min_dur_layout.addWidget(self.min_dur_spin)
        min_dur_layout.addStretch()
        edit_layout.addLayout(min_dur_layout)
        
        # 最大片段时长
        max_dur_layout = QHBoxLayout()
        max_dur_layout.addWidget(QLabel("最大片段时长(秒):"))
        self.max_dur_spin = QSpinBox()
        self.max_dur_spin.setRange(1, 120)
        self.max_dur_spin.setValue(10)
        max_dur_layout.addWidget(self.max_dur_spin)
        max_dur_layout.addStretch()
        edit_layout.addLayout(max_dur_layout)
        
        # 转场时长
        trans_layout = QHBoxLayout()
        trans_layout.addWidget(QLabel("转场时长(秒):"))
        self.trans_spin = QSpinBox()
        self.trans_spin.setRange(1, 5)
        self.trans_spin.setValue(1)
        trans_layout.addWidget(self.trans_spin)
        trans_layout.addStretch()
        edit_layout.addLayout(trans_layout)
        
        edit_group.setLayout(edit_layout)
        layout.addWidget(edit_group)
        
        # 添加开始生成按钮
        self.generate_btn = QPushButton("开始生成")
        self.generate_btn.setFixedHeight(40)
        self.generate_btn.setStyleSheet("""
            QPushButton {
                background-color: #2196F3;
                color: white;
                border-radius: 4px;
                font-size: 16px;
            }
            QPushButton:hover {
                background-color: #1976D2;
            }
        """)
        main_layout.addWidget(self.generate_btn)
        
        # 设置整体样式
        self.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                margin-top: 1ex;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 3px 0 3px;
            }
            QLabel {
                font-size: 14px;
            }
            QLineEdit, QSpinBox, QComboBox {
                padding: 5px;
                border: 1px solid #ccc;
                border-radius: 4px;
            }
            QPushButton {
                padding: 5px 10px;
                border: none;
                border-radius: 4px;
            }
        """)
        
    def upload_music(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "选择音乐文件",
            "",
            "音频文件 (*.mp3 *.wav *.ogg);;所有文件 (*.*)"
        )
        if file_name:
            # TODO: 处理音乐文件
            pass 